package com.wee.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.bigtotoro.util.SharedPreferencesUtil;
import com.bigtotoro.widgets.AppProgressBar;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.wee.adapter.CourseAdapter1;
import com.wee.aircoach_user.CoachPlanActivity;
import com.wee.aircoach_user.Constant;
import com.wee.aircoach_user.MyApplication;
import com.wee.aircoach_user.R;
import com.wee.entity.Coach_project;
import com.wee.entity.Coach_projectResponse;
import com.wee.entity.MD5Util;

import org.apache.http.Header;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Solomon on 2015/11/24.
 */
public class faxian_fragment2 extends Fragment{

    private View view;
    private ListView systemCoachList;
    private CourseAdapter1 courseAdapter;

    private List<Coach_project> projects = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.faxian_fragment2, container, false);
       initview();
        return view;
    }

    private void initview() {
        systemCoachList = (ListView)view.findViewById(R.id.list_kecheng);
        courseAdapter = new CourseAdapter1(getActivity(), projects);
        systemCoachList.setAdapter(courseAdapter);
        systemCoachList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = new Intent(getActivity(), CoachPlanActivity.class);
                intent.putExtra("CoachDown", projects.get(position).getId() + "");
                intent.putExtra("teacherId", projects.get(position).getCoach_id() + "");

                startActivity(intent);
            }
        });
   loadSystemCoac();

    }
    private void loadSystemCoac(){
        int exact=  SharedPreferencesUtil.getInt(getActivity(), Constant.USERTIME);
        RequestParams params = new RequestParams();
        AppProgressBar.checkAndCreateProgressBar(getActivity());
        MyApplication.getInstance().getHttpClient().get(Constant.COACH_PROJECT+ MD5Util.md5(exact), params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                try {
                Coach_projectResponse response = new Gson().fromJson(new String(bytes), Coach_projectResponse.class);
                if (response != null && response.getData().size() != 0) {
                    projects.clear();
                    projects.addAll(response.getData());
                    courseAdapter.notifyDataSetChanged();
                }
                AppProgressBar.closeProgressBar();
                }catch (Exception r){
                    Toast.makeText(getActivity(), "网络不稳定", Toast.LENGTH_SHORT).show();
                    AppProgressBar.closeProgressBar();
                }
            }
            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                throwable.printStackTrace();
                AppProgressBar.closeProgressBar();
            }
        });
    }
}
